#!/usr/bin/perl
use strict;

my ($ITER, $RANGE) = (200, 50);

sub compare_arrays(\@\@$)
{
	my ($arr1,  $arr2,  $name) = @_;
	if (scalar @$arr1 != scalar @$arr2)
	{
		die "FAILED $name ". scalar @$arr1. "!=". scalar @$arr2. "\n";;
	}
	for my $i (0.. (@$arr1 - 1) )
	{
		if ($arr1->[$i] ne $arr2->[$i])
		{
			print STDERR "$arr1->[$i] ne $arr2->[$i]\n";
			die "FAILED $name" ;
		}
	}
}


print STDERR "\tGenerating test files\n";
{
	open RESULTS, ">seq_pairs.test";
	open NORM_RESULTS, ">seq_pairs.norm.test";
	print RESULTS "seq1\tseq2\n----\t----\t----\n";
	my $cnt_results = 0;
	print STDERR "\t[";
	for (0..$ITER)
	{
		
		print STDERR "." if ($_ % ($ITER / 50) == 0);
		++$cnt_results;
		my $id1 =  int(rand($RANGE));
		my $id2 =  int(rand($RANGE));

		my $line = "0.1\t0.1\t0.1\t23\t24\t0\t0\t1\t1\t2\tresult$cnt_results".
						"\t0\t22\t21\t1\t0.1\t11\t12\t12\t100\t23\t1:1\t2:1";
		print RESULTS		"id_$id1\tid_$id2\t", $line, "\n";
		if ("id_$id1" lt "id_$id2")
		{
			print NORM_RESULTS		"id_$id1\tid_$id2\t", $line, "\n";
		}
		elsif ("id_$id1" gt "id_$id2")
		{
			$line = "0.1\t0.1\t0.1\t23\t24\t0\t0\t1\t1\t2\tresult$cnt_results".
					"\t0\t22\t21\t1\t0.1\t12\t11\t12\t100\t23\t2:1\t1:1";
			print NORM_RESULTS		"id_$id2\tid_$id1\t", $line, "\n";
		}
			
	}
	print STDERR "]\n";
}

print STDERR "\tRunning seq_pairs_normalize_ids\n\n";
{
	system("seq_pairs_normalize_ids -d -v -k -i seq_pairs.test  > seq_pairs.renorm.test");
}
print STDERR "\n\n";







print STDERR "\tComparing results...\n";
{
	my @diff = `diff seq_pairs.renorm.test seq_pairs.norm.test`;
	if (@diff != 0)
	{
		print STDERR "FAILED: seq_pairs.renorm.test seq_pairs.norm.test are not the same!\n";
		exit;
	}
}






print STDERR "\tCleaning up...\n";
unlink (glob("*.test"));
print STDERR "\n\tSUCCESS!!!\n\n";

